package com.example.wan1.view.todo;

import com.example.wan1.bus.RxBus;
import com.example.wan1.data.IsNotObserver;
import com.example.wan1.data.RetrofitUtil;
import com.example.wan1.data.RxSchedulers;
import com.example.wan1.event.NotifyOperateEvent;
import com.example.wan1.utils.ToastUtils;

/***
 * 创建时间：2020/8/1 8:43
 * 创建人：10850
 * 功能描述：todo学习
 */
@SuppressWarnings("unchecked")
public class TodoHandleTool {

    private volatile static TodoHandleTool instance;

    public static TodoHandleTool getInstance() {
        if (instance == null) {
            synchronized (TodoHandleTool.class) {
                if (instance == null) {
                    instance = new TodoHandleTool();
                }
            }
        }
        return instance;
    }

    public void addTodo(String title, String content, String date, int type, int priority) {
        RetrofitUtil.getInstance().addTodo(title, content, date, type, priority)
                .compose(RxSchedulers.observableIO2Main())
                .subscribe(new IsNotObserver() {
                    @Override
                    public void onSuccess(int code, String result) {
                        ToastUtils.showToast("添加清单成功！");
                        RxBus.getDefault().post(new NotifyOperateEvent("todo"));
                    }

                    @Override
                    public void onFailure(Throwable e, String errorMsg) {
                        ToastUtils.showToast(e.getMessage());
                    }
                });
    }

    public void updateTodo(int id, String title, String content, String date, int status, int type, int priority) {
        RetrofitUtil.getInstance().updateTodo(id, title, content, date, status, type, priority)
                .compose(RxSchedulers.observableIO2Main())
                .subscribe(new IsNotObserver() {
                    @Override
                    public void onSuccess(int code, String result) {
                        ToastUtils.showToast("更新清单成功！");
                        RxBus.getDefault().post(new NotifyOperateEvent("todo"));
                    }

                    @Override
                    public void onFailure(Throwable e, String errorMsg) {
                        ToastUtils.showToast(e.getMessage());
                    }
                });
    }

    public void deleteTodo(int id) {
        RetrofitUtil.getInstance().deleteTodo(id)
                .compose(RxSchedulers.observableIO2Main())
                .subscribe(new IsNotObserver() {
                    @Override
                    public void onSuccess(int code, String result) {
                        ToastUtils.showToast("删除清单成功！");
                        RxBus.getDefault().post(new NotifyOperateEvent("todo"));
                    }

                    @Override
                    public void onFailure(Throwable e, String errorMsg) {
                        ToastUtils.showToast(e.getMessage());
                    }
                });
    }

    public void updateTodoStatus(int id, int status) {
        RetrofitUtil.getInstance().updateTodoStatus(id, status)
                .compose(RxSchedulers.observableIO2Main())
                .subscribe(new IsNotObserver() {
                    @Override
                    public void onSuccess(int code, String result) {
                        ToastUtils.showToast("更新完成!");
                        RxBus.getDefault().post(new NotifyOperateEvent("todo"));
                    }

                    @Override
                    public void onFailure(Throwable e, String errorMsg) {
                        ToastUtils.showToast(e.getMessage());
                    }
                });
    }
}
